﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLKDS.DAL
{
    public class CategoryBookManager
    {
        public IList<CategoryBookInfo> ListCategoryBook()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCategoryBook = from d in db.CategoryBooks
                                   select new CategoryBookInfo { Id = d.Id,BookId=d.BookId,CategoryId=d.CategoryId};

                return queryCategoryBook.ToList();
            }
        }


        public void CreateCategoryBook(CategoryBook CategoryBook)
        {
            using (var db = new QLKDSDataContext())
            {
                db.CategoryBooks.InsertOnSubmit(CategoryBook);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="CategoryBook"></param>
        public void UpdateCategoryBook(CategoryBook CategoryBook)
        {
            using (var db = new QLKDSDataContext())
            {
                var _CategoryBook = db.CategoryBooks.Single(d => d.Id.Equals(CategoryBook.Id));

                _CategoryBook.BookId = CategoryBook.BookId;
                _CategoryBook.CategoryId = CategoryBook.CategoryId;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteCategoryBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _CategoryBook = db.CategoryBooks.Single(d => d.Id.Equals(id));

                db.CategoryBooks.DeleteOnSubmit(_CategoryBook);
                db.SubmitChanges();
            }
        }


    }
}
